(function() {
    'use strict';

    /** 预约项目类型管理
     * @ngdoc function
     * @name webApp.controller:RestypeCtrl
     * @description 
     * # RestypeCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('RestypeCtrl', RestypeCtrl);

    RestypeCtrl.$inject = ['$scope', 'dataService', 'helper', 'dhfprinterService', '$timeout', '$interval', '$rootScope', '$state', 'moment', '$location'];

    function RestypeCtrl($scope, dataService, helper, dhfprinterService, $timeout, $interval, $rootScope, $state, moment, $location) {
        $scope.listAction = function() {
            $scope.getlist(1);
            $scope.$on('event:up-store-id', function() {
                $scope.getlist(1);
            });
        }
        
        $scope.getlist = function(pageNo) {
            $scope.pageNo = pageNo ? pageNo : "1";
            var query = {
                page_no: $scope.pageNo,
                page_size: $scope.pageSize,
                query: { store_id: $rootScope.store_id }
            };
            dataService.restypeGet(query).then(function(re1) {
                if (re1.res_types) {
                    $scope.res_types = re1.res_types;
                    $scope.total_count = re1.total_count;
                }
            });
        }

        $scope.getone = function() {
            var query = {
                query: { store_id: $rootScope.store_id, res_type_id: $scope.res_type_id }
            };
            dataService.restypeGet(query).then(function(re1) {
                if (re1.res_types) {
                    $scope.res_type = re1.res_types[0];
                    $scope.res_type.parent = $scope.res_type.res_type_id;
                }
            });
        }


        //添加数据 初始化
        $scope.addAction = function() {
            $scope.res_type_id = $location.search().res_type_id;
            if ($scope.res_type_id) {
                $scope.getone();
            } else {
                $scope.res_type = { name: null };
            }
        }

        $scope.operation = function(id,type, title) {
            helper.myConfirm("确定" + title, function() {
                var data = {"res_type_id":id};
                $scope.postapi(type,data);
            });
        }

        $scope.saveData = function() {
            $scope.res_type.store_id = $rootScope.store_id;
            $scope.res_type.parent_id = $scope.res_type.parent ? $scope.res_type.res_type_id ? ($scope.res_type.parent == $scope.res_type.res_type_id ? $scope.res_type.parent_id : $scope.res_type.parent) : $scope.res_type.parent : 0;
            var api = $scope.res_type_id ? "up" : "add";
            $scope.postapi(api);
        }

        $scope.postapi = function(type,da) {
            var zhuoziApiAry = {
                "add": "restypeAdd",
                "up": "restypeUpdate",
                "del": "restypeDelete",
            }
            var data = type!=='del'? clone($scope.res_type):da;
            dataService[zhuoziApiAry[type]](data).then(function(r) {
                if (r.Msg) {
                    helper.myAlert(r.Msg);
                } else {
                    helper.myAlert("success", null, null, function() {
                        $scope.getlist(1);
                        $state.go("restype.list");
                    });
                }

            })
        }
    }
})();